﻿using AutoMapper;
using Common.Communal;
using ERP.Domain.System;
using MediatR;
using System.Api.Write.Applications.Commands.Permissions;
using System.Services.Permissions;

namespace System.Api.Write.Applications.CommandHandlers.Permissions
{
    /// <summary>
    /// 更新权限命令处理程序
    /// </summary>
    public class PermissionUpdateCommandHandler : IRequestHandler<PermissionUpdateCommand, ApiResult<int>>
    {
        /// <summary>
        /// 权限服务
        /// </summary>
        private readonly IPermissionService service;
        /// <summary>
        /// 对象映射器
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造函数，注入权限服务
        /// </summary>
        /// <param name="service">权限服务</param>
        /// <param name="mapper">对象映射器</param>
        public PermissionUpdateCommandHandler(IPermissionService service,IMapper mapper)
        {
            this.service = service;
            this.mapper = mapper;
        }
        /// <summary>
        /// 处理更新权限命令
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回结果</returns>
        public async Task<ApiResult<int>> Handle(PermissionUpdateCommand request, CancellationToken cancellationToken)
        {
            var res=mapper.Map<Permission>(request);
            return await service.Update(res);
        }
    }
}
